# frozen_string_literal: true

ActiveAdmin.register Schema do
  menu parent: ['Schema'], priority: 1

  permit_params do
    permitted = %i[name description]
    permitted
  end

  filter :name
  filter :status, as: :check_boxes, collection: proc { Schema::Status.values }
  filter :description

  index do
    selectable_column
    id_column
    column 'UUID', :uuid
    column 'Name', :name
    column 'Description', :description
    column 'Status', 'status'

    actions defaults: false do |schema|
      item 'View', admin_schema_path(schema)
    end
  end

  form do |f|
    f.inputs do
      f.input :name
      f.input :description
    end
    f.actions
  end

  show do
    attributes_table do
      row :uuid
      row :name
      row :description
      row :status
      row :created_at
      row :updated_at
    end

    panel 'Inputs' do
      variables = schema.input_variables.ordinal_asc.page(params[:page]).per(10)

      paginated_collection(variables, download_links: false) do
        table_for variables.includes(:variable_type) do
          column 'Variable Type' do |variable|
            link_to variable.variable_type.name, admin_variable_type_path(variable.variable_type)
          end
          column :name
          column :ordinal
          column :io_type
          column :required
          column :default_value

          column 'Actions' do |variable|
            span class: 'inline-span' do
              link_to 'View', admin_schema_variable_path(variable)
            end
          end
        end
      end
    end

    panel 'Outputs' do
      variables = schema.output_variables.ordinal_asc.page(params[:page]).per(10)

      paginated_collection(variables, download_links: false) do
        table_for variables.includes(:variable_type) do
          column 'Variable Type' do |variable|
            link_to variable.variable_type.name, admin_variable_type_path(variable.variable_type)
          end
          column :name
          column :ordinal
          column :io_type
          column :required
          column :default_value

          column 'Actions' do |variable|
            span class: 'inline-span' do
              link_to 'View', admin_schema_variable_path(variable)
            end
          end
        end
      end
    end
  end

  collection_action :filter, method: :post do
    # Do some CSV importing work here...
    # redirect_to collection_path, notice: "CSV imported successfully!"
  end
end
